package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityIncomeAndExpanditureBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterIncomeAndExpenditure
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/7/20 8:46
 * @Describe    : 公棚收入支出
 */
class ActivityShedIncomeAndExpenditure : BaseActivity<OrgRaceViewModel, ActivityIncomeAndExpanditureBinding>() {

    private var mMemberId = 0
    private val mAdapter: AdapterIncomeAndExpenditure by lazy { AdapterIncomeAndExpenditure(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "公棚收入支出"

        mMemberId = intent.getIntExtra("memberId", 0)
        val raceName = intent.getStringExtra("raceName")
        val memberName = intent.getStringExtra("memberName")
        val season = intent.getStringExtra("season")
        mDatabind.tvSeasonName.text = season
        mDatabind.tvRaceName.text = raceName
        mDatabind.tvMemberName.text = memberName

        onRefresh()

        mDatabind.swipeRefreshLayout.init {
            onRefresh()
        }

        mDatabind.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getRaceMemberAllMoney(mMemberId)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.memberAllMoneyResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.includeEmptyView.llEmptyData.isVisible = it.isEmpty()
            mDatabind.swipeRecyclerView.isVisible = !it.isEmpty()
            mDatabind.data = it
            mAdapter.setList(it.list)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}